home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12908 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: EU.net!sun4nl!xs4all!falstaff
  2. From: falstaff@xs4all.nl (Falstaff)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: routine for yesterday's date
  5. Date: 3 Apr 1996 14:27:44 GMT
  6. Organization: XS4ALL, networking for the masses
  7. Message-ID: <4ju1t0$p2q@news.xs4all.nl>
  8. References: <315c4d0f.19874776@ottnews.shl.com> <1996Mar30.043002.19054@sq.com> <Dp3B56.FKI@alisa.org> <1996Mar31.062512.22480@sq.com> <31614471.2351449@ottnews.shl.com>
  9. NNTP-Posting-Host: xs1.xs4all.nl
  10. X-Newsreader: NN version 6.5.0 #666 (NOV)
  11.  
  12. 75323.455@compuserve.com (Bruce Coghill) writes:
  13.  
  14. >Anyway, I did manage to implement a solution using similar code from
  15. >this thread from user (Falstaff).  It compiles and I will be testing
  16. >it today, I hope :~+ 
  17.  
  18. >/*******************************************/
  19. >int DIM[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  20.  
  21. >if(!--d)
  22. >{  
  23. >   if(!--m)
  24. >   {  m=12;
  25. >      y--; }
  26.  
  27. >   DIM[1]=(~y&3 && (y%100||!(y%400)))?29:28; /* See note below */
  28. >   d=DIM[m-1];
  29. >}
  30. >/***************************************/
  31.  
  32. >The DIM[1]=  line I understand everything after the &&, however not
  33. >before.  I know the unary (~), logical (&) and the other and (&&), but
  34. >why to we and the year with 3 then change the bits?  
  35.  
  36. I wasn't thinking clearly there...  I should have written
  37.  
  38.    DIM[1]=(y%4==0 && (y%100||!(y%400)))?29:28;
  39.  
  40. ~y&3 evaluates to nonzero if y=3,7,11... etc.  That's nice for
  41. some things, but not here.
  42. The expression before the ? breaks up as follows:
  43.  
  44.    y%4==0        year a multiple of four
  45.    &&            and
  46.    (  y%100        it is not a multiple of 100
  47.       ||        or
  48.       !(y%400)        it is a multiple of 400
  49.    )
  50.  
  51. This all means that DIM[1] is set to 29 if above conditions hold (when
  52. it's a leap year) and to 28 if they dont.
  53.  
  54. Falstaff
  55. --
  56. The famous GIICM now on line:  http://www.xs4all.nl/~falstaff/GIICM.html
  57. ------------------------------------------------------------------------
  58. Frank A. Vorstenbosch        +31-(70)-355 5241        falstaff@xs4all.nl
  59.